<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentSubscribe.class.php, $Author:  $ */



/**
 * Component for subscribe membership
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentSubscribe.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 */
class PhpFox_Mod_Account_ComponentSubscribe extends BaseComponent
{
	/** Processing
	 */
	function process()
	{
		if ( !extension_loaded('curl') )
		{
			return App::format('Mod_Account.no_curl_warning');
		}

	    App::loadClass('payments/payment');
	    $oSubscribeService = $this->_oModule->getService('Subscribe');
	    /* @var $oSubscribeService PhpFox_Mod_Account_ServiceSubscribe */

	    $oSrvAccount = $this->_oModule->getService('Account');
        /* @var $oSrvAccount PhpFox_Mod_Account_ServiceAccount */
	    if(App::getSetting('sub_system') != '1')
	    {
             App::gotoUrl('public.denied');
	    }

		$oReq = &App::getRequest();

	    $iActiveDays = 0;
	    $oneTimePayMembership = '';
	    $iMembershipId = 0;
	    $sAction = $oReq->getAction();
	    if ($sAction == 'notify')
	    {
	        $oSubscribeService->processPostback();
	    }

        $oUser = $this->getParam('user');
        $aUserMembership = array();
	    $oSrvSec = $this->_oModule->getService('Security');
	    /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */

	    $bStaff = $oSrvSec->isStaff();
        $oTpl = &$this->_oModule->getTemplate();

		if (!isset($oUser->aData['id']))
        {
            $bActive = false;
		    $iActiveFor = 0;
		    $aUserMembership = $oSrvSec->getMembershipData(App::getParam('Mod_Account.membership.guest'));
		    $aUserMembership['is_internal'] = true;
		    $bIsNormal = false;
		    App::gotoUrl('public.login');
        }
        else
        {
		    //process subscribe button
		    $iMembership_id = $oReq->getInt('membership_id', 0);
		    $bAcceptNewMembershipNow = ($oReq->getInt('accept_now')) ? true :  false;

			if ($iMembership_id)
		    {
		        if ($bStaff)
		        {
		            App::gotoUrl('public.subscribe');
		        }
		        $aMData = $oSrvSec->getMembershipData($iMembership_id);
		        if ($aMData['is_internal'] == 0 && $sAction == 'btnPay')
		        {
                    if ($aMData['price'] == 0)
                    {
                        $oSrvAccount->setMembership($oUser->aData['id'], $iMembership_id, '', true);
                        App::gotoUrl('subscribe',array('act'=>'success'));
                    }
                    else
                    {
    				    $oSubscribeService->addTransaction($oUser, $aMData, $bAcceptNewMembershipNow);
                    }
		        }
		    }

		    //is payment active yet
		    $bActive = false;
		    $iActiveFor = 0;
		    $aUserMembership = array();
		    if(isset($oUser->aData['last_payment']) AND (strtotime($oUser->aData['last_payment']) - time() > 0))
		    {
		        $bActive = true;
		        $iMembershipId = $oUser->aData['type'];
			    $oneTimePayMembership='No';
                if ((strtotime($oUser->aData['last_payment'])-time())>315360000) // 10 years
                {
                    $oneTimePayMembership='Yes';
                }
		        $iActiveFor = ceil((strtotime($oUser->aData['last_payment']) - time()) / 86400); //(days)
			    $iActiveDays = floor((time()-strtotime('-1 month',strtotime($oUser->aData['last_payment'])))/ 86400);
		    }
		    $aUserMembership = $oSrvSec->getMembershipData($oUser->aData['type']);
		    $bIsNormal = ($aUserMembership['membership_id'] == App::getParam('Mod_Account.membership.normal'));
        }

        list($aMemberships, $aCached) = $oSrvSec->getMembershipsList();

		$oLang = &App::getLanguage();
		$sIntro = $oLang->getContent('subscribe');

	    $oTpl->assignSrc(array(
	    	'sIntro' => $sIntro,
	    ));

	    $oTpl->assign(array(
    		'iActiveFor'           => $iActiveFor,
    		'iActiveDays'          => $iActiveDays,
    		'oneTimePayMembership' => $oneTimePayMembership,
	        'bIsNormal'            => $bIsNormal,
            'bActive'              => $bActive,
	        'aUserMembership'      => $aUserMembership,
	        'bStaff'               => $bStaff,
	        'bVisitor'             => (!$oUser) ? true : false,
	        'iMembershipId'        => $iMembershipId,
	        'iActiveFor'           => $iActiveFor,
			'aMemberships'         => $aMemberships,
	        'sAction'              => $sAction,
	        'sCurrency'            => App::getSetting('payment_paypal_currency'),
	    ));


		$srcHtml = $oTpl->fetch('Subscribe.html');
		return $srcHtml;
	}
}
?>